
##### R CODE TO REPLICATE FIGURES 1 AND 2
##### Sergio J. Ascencio and Yann P. Kerevel, "Party Strategy, Candidate Selection and Legislative Behavior in Mexico," forthcoming at Legislative Studies Quarterly

### Loading the required R packages. 
library(foreign)
library(ggplot2)


### Working directory -> change this to the directory where your have the data (Figure1.dta, Figure2.dta, ElectoralData.RData)
setwd("/Users/Sergio/Dropbox/00Papers/PaperYann/FiguresLSQ")

## Load data (output from Stata models)
data1 <- read.dta("Figure1.dta")
data2 <- read.dta("Figure2.dta")



### FIGURE 1

ggfigure1 <- ggplot(data1, aes(x = Type)) +
  ggtitle("Figure 1. Electoral system, party competition, and roll call defections\n (Deputies appointed by national party leaders)") +
  geom_pointrange(aes(y = Margin, ymin = Lower, ymax = Upper)) +
  scale_x_discrete("", labels = c("PR", "SMD\n Stronghold", "SMD\n Competitive")) +
  scale_y_continuous("Predicted number of roll-call defections \n", limits = c(0, 8)) +
  theme_bw() +
  theme(title = element_text(size = 10),
        strip.text.x = element_text(size=12),
        strip.text.y = element_text(size=12),
        axis.title.x = element_text(size=12),
        axis.title.y = element_text(size=12),
        axis.text.y = element_text(size=12),
        axis.text.x = element_text(size=12),
        legend.direction = "horizontal", legend.position = "bottom",
        legend.text = element_text(size=11),
        legend.title = element_text(size=11)) +
  guides(color = guide_legend(title.position="top", title.hjust=.5, keywidth = 2, keyheight = 1, reverse=FALSE))

ggfigure1

# Save, dimensions 6 x 4.5 in
ggsave("Figure1.pdf", width = 6, height = 4.5, units = "in")



### FIGURE 2

# Load electoral data for rugs
load("ElectoralData.RData")
dataAppoint <- subset(datos, Primary == 0)
dataPrimary <- subset(datos, Primary == 1)

ggfigure2 <- ggplot(data2, aes(x = Margin)) +
  ggtitle("Figure 2. Nomination rules, party competition, and roll call defections\n (Deputies elected in single-member districts)") +
  geom_line(aes(y = D, col = as.factor(Type), lty = as.factor(Type)), lwd = 1.2) +
  geom_ribbon(aes(ymin = Lower, ymax = Upper, fill = as.factor(Type)), alpha=0.3) +
  geom_rug(data = dataAppoint, sides = "b", col = "grey50") +
  geom_rug(data = dataPrimary, sides = "t", col = "black") +
  scale_x_continuous("\n Average margin of victory in previous two elections", limits = c(-0.35, 0.5)) +
  scale_y_continuous("Predicted number of roll-call defections \n", limits = c(0, 17.5)) +
  scale_colour_manual("Nomination rule", values= c("grey50","black")) +
  scale_fill_manual("Nomination rule", values= c("grey50","black")) +
  scale_linetype_manual("Nomination rule", values= c("dashed","solid")) +
  theme_bw() +
  theme(title = element_text(size = 10),
        strip.text.x = element_text(size=12),
        strip.text.y = element_text(size=12),
        axis.title.x = element_text(size=12),
        axis.title.y = element_text(size=12),
        axis.text.y = element_text(size=12),
        axis.text.x = element_text(size=12),
        legend.direction = "horizontal", legend.position = "bottom",
        legend.text = element_text(size=11),
        legend.title = element_text(size=11)) +
  guides(color = guide_legend(title.position="top", title.hjust=.5, keywidth = 2, keyheight = 1, reverse=FALSE))
ggfigure2

# Save, dimensions 6 x 4.5 in
ggsave("Figure2.pdf", width = 6, height = 4.5, units = "in")
